<template>
  <div class="buttonAddWrap">
    <el-dialog @opened="$store.commit('dialogTop')" title="编辑投放规划指标" :visible.sync="dialogFormVisibleAdd" @close="addDialogClose('ruleFormAdd')">
      <el-form :model="ruleFormAdd" :rules="rulesAdd" ref="ruleFormAdd" label-width="80px" class="demo-ruleForm">
        <el-form-item label="指标编号" prop="batchNo" :rules="[{ 'required':true, 'message': '请输入指标编号','trigger': 'blur' }]">
          <el-input clearable size="mini" v-model="ruleFormAdd.batchNo" type="number"
                    auto-complete="off" placeholder="指标编号"></el-input>
        </el-form-item>
        <el-form-item label="指标配额" prop="putCount" :rules="[{ 'required':true, 'message': '请输入指标配额','trigger': 'blur' }]">
          <el-input clearable size="mini" v-model="ruleFormAdd.putCount" type="number"
                    auto-complete="off" placeholder="指标配额"></el-input>
        </el-form-item>
        <el-form-item label="投放区域" prop="putStreet" :rules="[{ 'required':true, 'message': '请选择区域','trigger': 'blur' }]">
          <el-row>
            <el-col :span="12">
              <el-select :disabled="$store.state.userInfo.regionId?true:false" v-model="ruleFormAdd.putDistrict" @change="changeDistrict(ruleFormAdd.putDistrict)" size="mini"
                         clearable placeholder="行政区域">
                <el-option
                  v-for="item in $store.state.districtData"
                  :key="item.id"
                  :label="item.regionName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="12">
              <el-select v-model="ruleFormAdd.putStreet" size="mini" clearable placeholder="街道">
                <el-option
                  v-for="item in streetData"
                  :key="item.id"
                  :label="item.regionName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="有效日期" prop="effectiveBegin" :rules="[{ 'required':true, 'message': '请选择有效日期','trigger': 'blur' }]">
          <el-date-picker
            style="width: 100%;"
            v-model="value6"
            type="daterange"
            align="left"
            @change="getChangeTime"
            placeholder="选择日期范围"
            size="mini"
            :picker-options="$store.state.pickerOptions1"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="中标企业" prop="enterpriseNo" :rules="[{ 'required':true, 'message': '请选择企业','trigger': 'blur'  }]">
          <el-select v-model="ruleFormAdd.enterpriseNo" size="mini"  clearable placeholder="请选择企业">
            <el-option
              v-for="item in this.$store.state.companyInfos"
              :key="item.enterpriseNo+''"
              :label="item.enterpriseName"
              :value="item.enterpriseNo+''">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="中标方式" prop="bidType" :rules="[{ 'required':true, 'message': '中标方式', 'trigger': 'blur'}]">
          <el-radio v-model="ruleFormAdd.bidType" :label="0">分配</el-radio>
          <el-radio v-model="ruleFormAdd.bidType" :label="1">考核</el-radio>
          <el-radio v-model="ruleFormAdd.bidType" :label="2">抽取</el-radio>
        </el-form-item>
        <el-form-item label="上传标书" prop="fileId" :rules="[{ 'required':true, 'message': '请上传标书', }]">
          <uploadDialog @getfileId="getfileId"></uploadDialog>
          <el-button  size="small" type="text" @click="downLoadFile" style="margin-left: 20px;" >下载标书</el-button>
        </el-form-item>
        <el-form-item> </el-form-item>
        <div style="text-align: center;margin-bottom: 6px;">
          <el-button type="primary" @click="submitForm('ruleFormAdd')">提交</el-button>
          <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import uploadDialog from "./uploadDialog.vue"
  export default {
    props:[],
    data () {
      return {
        options1: [{
          value: '1',
          label: '市交通局'
        }, {
          value: '2',
          label: '市交警局'
        }, {
          value: '3',
          label: '市城管局'
        }, {
          value: '4',
          label: '区政府'
        }],
        value6:[],
        streetData: [],
        ruleFormAdd: {
          "id":"",
          "batchNo":"",
          "enterpriseNo":"",
          "effectiveBegin":"",
          "effectiveEnd":"",
          "bidType":0,
          "putCount":"",
          "putDistrict":this.$store.state.userInfo.regionId,
          "putStreet":"",
          "fileId":""
        },
        downloadPath:"",
        dialogFormVisibleAdd: false,
        rulesAdd: {

        }
      }
    },
    created(){

    },
    components: {uploadDialog},
    methods: {
      downLoadFile(){
        if(this.downloadPath){
          window.location.href=this.downloadPath
        }else {
          this.$message({
            type:"warning",
            message:"无标书下载!"
          })
        }

      },
      //获取街道
      changeDistrict(district){
        if(!district){
          this.ruleFormAdd.putStreet=""
          this.streetData=[]
          return
        }
        this.axios.post(process.env.API_HOST + '/common/selectRegion',{parentId:district})
          .then(response => {
            const result = response.data
            if (result.code == 200) {
              this.streetData = result.result
            }
          })
      },
      dialogOpen(id){
        this.axios.post(process.env.API_HOST + '/put/selectPutRecordDetail',{id:id})
          .then(response => {
            const result = response.data
            if (result.code == 200) {
              this.value6=[]
              this.downloadPath=result.result.downloadPath
              for(var i in result.result){
                this.ruleFormAdd[i]=result.result[i]
              }
              this.value6[0]=this.ruleFormAdd.effectiveBegin
              this.value6[1]=this.ruleFormAdd.effectiveEnd
              this.changeDistrict(this.ruleFormAdd.putDistrict)
              this.dialogFormVisibleAdd = true
            }
          })

      },
      getfileId(id){
        this.ruleFormAdd.fileId=id
      },
      //关闭回调
      addDialogClose(formName){
        this.$refs[formName].resetFields();
      },
      //提交回调
      handleDiaSureAdd(){

        this.axios.post(process.env.API_HOST + `/put/updatePutRecord`, this.ruleFormAdd)
          .then(response=>{
            if(response.data.code==200){
              this.$message({
                type: 'success',
                message: '编辑成功!'
              });
              this.$emit('searchRowsInfo')
              this.dialogFormVisibleAdd = false
            }

          })
          .catch(error=>{
            console.log(error);
          });
      },
      getChangeTime(){
        if (this.value6&&this.value6[0] != null) {
          this.ruleFormAdd.effectiveBegin = this.dtime(this.value6[0]).format('YYYY-MM-DD 00:00:00')
          this.ruleFormAdd.effectiveEnd = this.dtime(this.value6[1]).format('YYYY-MM-DD 23:59:59')
        } else {
          this.ruleFormAdd.effectiveBegin = ""
          this.ruleFormAdd.effectiveEnd = ""
        }
      },
      submitForm(formName) {

        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.handleDiaSureAdd()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus" >
  @import "../../../common/stylus/mixin.styl"
  .buttonAddWrap
    display inline-block
    margin-left 10px

</style>
